// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        NEW_LEFT: false,
        NEW_RIGHT: false,
        NEW_UP: false,
        NEW_DOWN: false,
        speed: {
            default: 30,
            displayName: "new Player speed"
        },
        anim: null,
        animState: null,
        hook_ani:{
            default:null,
            type:cc.Node
        }
    },

    // LIFE-CYCLE CALLBACKS:
    onLoad() {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
        this.anim = this.getComponent(cc.Animation);
    },
    onKeyDown: function (event) {
        // var anim = this.getComponent(cc.Animation);
        switch (event.keyCode) {
            case cc.macro.KEY.up:
                this.NEW_UP = true;
                this.hook_ani.runAction(cc.rotateTo(0,0));
                if (this.animState == null || (this.animState.isPlaying == false)) {
                    this.animState = this.anim.play("PlayerW");
                    this.animState.wrapMode = cc.WrapMode.Loop;
                    this.animState.repeatCount = Infinity;
                } else {
                    break;
                }
                break;
            case cc.macro.KEY.left:
                this.NEW_LEFT = true;
                this.hook_ani.runAction(cc.rotateTo(0,270));
                if (this.animState == null || (this.animState.isPlaying == false)) {
                    this.animState = this.anim.play("PlayerA");
                    this.animState.wrapMode = cc.WrapMode.Loop;
                    this.animState.repeatCount = Infinity;
                } else {
                    break;
                }
                break;
            case cc.macro.KEY.down:
                this.NEW_DOWN = true;
                this.hook_ani.runAction(cc.rotateTo(0,180));
                if (this.animState == null || (this.animState.isPlaying == false)) {
                    this.animState = this.anim.play("PlayerS");
                    this.animState.wrapMode = cc.WrapMode.Loop;
                    this.animState.repeatCount = Infinity;
                } else {
                    break;
                }
                break;
            case cc.macro.KEY.right:
                this.NEW_RIGHT = true;
                this.hook_ani.runAction(cc.rotateTo(0,90));
                if (this.animState == null || (this.animState.isPlaying == false)) {
                    this.animState = this.anim.play("PlayerD");
                    this.animState.wrapMode = cc.WrapMode.Loop;
                    this.animState.repeatCount = Infinity;
                } else {
                    break;
                }
                break;
            case cc.macro.KEY.g:
                this.hook_ani.getComponent(cc.Animation).play();
                break;
        }
    },
    onKeyUp: function (event) {
        switch (event.keyCode) {
            case cc.macro.KEY.up:
                this.NEW_UP = false;
                this.anim.stop("PlayerW");
                break;
            case cc.macro.KEY.left:
                this.NEW_LEFT = false;
                this.anim.stop("PlayerA");
                break;
            case cc.macro.KEY.down:
                this.NEW_DOWN = false;
                this.anim.stop("PlayerS");
                break;
            case cc.macro.KEY.right:
                this.NEW_RIGHT = false;
                this.anim.stop("PlayerD");
                break;
        }
    },

    start() {

    },

    update(dt) {
        // console.log("update..");
        var x = 0;
        var y = 0;
        var animState = null;
        if (this.NEW_LEFT) {
            // console.log("left");
            x = x - 30 * dt;
            // animState = this.anim.play("PlayerA");
        }
        if (this.NEW_RIGHT) {
            // console.log("right");
            x = x + 30 * dt;
            // animState = this.anim.play("PlayerD");
        }
        if (this.NEW_UP) {
            // console.log("up");
            y = y + 30 * dt;
            // animState = this.anim.play("PlayerW");
        }
        if (this.NEW_DOWN) {
            // console.log("down");
            y = y - 30 * dt;
            // animState = this.anim.play("PlayerX");
        }
        // if (animState != null) {
        //     // animState.wrapMode = cc.WrapMode.Loop;
        //     // animState.repeatCount = Infinity;
        //     this.node.runAction(cc.moveBy(0.1, cc.v2(x, y)));
        // }
        this.node.runAction(cc.moveBy(0.1, cc.v2(x, y)));
    },
});
